#!/bin/bash

echo "========== composer checking ============"
composer clearcache
composer install
composer update
composer fund

echo "========== renew logrotate setting ============"
cp ./deploy/php_cli /etc/logrotate.d/
logrotate /etc/logrotate.conf

echo "========== prepare start service ============="
if [ ! -e "/var/log/php-cli" ]; then
    mkdir -p /var/log/php-cli
fi 
if [ ! -e "/var/log/php-cli/service.log" ]; then
    touch /var/log/php-cli/service.log
fi 
if [ ! -e "/var/log/php-cli/error.log" ]; then
    touch /var/log/php-cli/error.log
fi 

echo "========== ready to start service =============="
## avoid error: extra characters at the end of d command
cd config
sed -i "s/'hostname'.*/'hostname'        => '43.132.247.193',/g" database.php
sed -i "s/'database'.*/'database'        => 'db_venom_live',/g" database.php
sed -i "s/'username'.*/'username'        => 'db_venom_live',/g" database.php
sed -i "s/'hostname'.*/'password'        => '#EDC2wsx!QAZ',/g" database.php

sed -i "s/'host'.*/'host'	=>	'demo.redis',/g" cache.php
cd ..

nohup php think run --port 8001 3>&1 2>>/var/log/php-cli/error.log | tee -a /var/log/php-cli/service.log &
